﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace UI.Themes
{
    /// <summary>
    /// 主题切换类
    /// </summary>
    public partial class ThemeChoose : ObservableObject
    {
        /*  Demo In MainWindow.xaml
         *  
                <Grid Background="{DynamicResource BackColor}">
                    <StackPanel
                        Height="60"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Top"
                        Orientation="Horizontal">
                        <Button
                            Margin="12"
                            Command="{Binding ChangeThemeCommand}"
                            CommandParameter="默认"
                            Content="默认" />
                        <Button
                            Margin="12"
                            Command="{Binding ChangeThemeCommand}"
                            CommandParameter="红色"
                            Content="红色" />
                        <Button
                            Margin="12"
                            Command="{Binding ChangeThemeCommand}"
                            CommandParameter="黄色"
                            Content="黄色" />
                        <Button
                            Margin="12"
                            Command="{Binding ChangeThemeCommand}"
                            CommandParameter="黑色"
                            Content="黑色" />
                    </StackPanel>                                               
                </Grid>
         *
         */

        /// <summary>
        /// 自动导入的主题字典
        /// 如果没有自主导入的主题字典，那么就必须由用户手动编写本页面的代码了。
        /// 87行之后的代码。
        /// 
        /// </summary>
        private List<ThemeItem> themes = new List<ThemeItem>();

        /// <summary>
        /// 无参数构造
        /// </summary>
        public ThemeChoose()
        {
        }
        /// <summary>
        /// 有参数构造
        /// </summary>
        /// <param name="_themes">把定义的主题传递进来</param>
        public ThemeChoose(List<ThemeItem> _themes)
        {
            ThemesInitialization(_themes);
        }


        public ThemeChoose(string JsonPath)
        {
            var str = File.ReadAllText(JsonPath);
            var _themes = Tools.JsonHelper.ToObject<List<ThemeItem>>(str);

            ThemesInitialization(_themes);
        }


        /// <summary>
        /// 【外部调用】切换主题。
        /// </summary>
        /// <param name="themeName"></param>
        [RelayCommand]
        public void ChangeTheme(string themeName)
        {
            //MessageBox.Show(themeName);

            var dictionaryPathName = "";
            if (themes.Count == 0)
            {
                //如果主题字典没有定义，那久需要手动写在这里了。
                #region 手动写入主题字典，并以此为依据进行选择。

                switch (themeName)
                {
                    case "红色":
                        {
                            dictionaryPathName = "/Theme/DictionaryColorRed.xaml";
                        }
                        break;
                    case "黄色":
                        {
                            dictionaryPathName = "/Theme/DictionaryColorYellow.xaml";
                        }
                        break;
                    case "黑色":
                        {
                            dictionaryPathName = "/Theme/DictionaryColorBlack.xaml";
                        }
                        break;
                    case "默认":
                    default:
                        {
                            dictionaryPathName = "/Theme/DictionaryColorDefault.xaml";
                        }
                        break;
                }
                #endregion

            }
            else
            {
                var temp = themes.Where(o => o.Key == themeName).FirstOrDefault();
                if (temp != null)
                {
                    dictionaryPathName = temp.Path;
                }
                else
                {
                    var msg = "主题切换时，命令值没有对应主题，请程序员自己注意检查。";
                    //throw new Exception(msg);
                    MessageBox.Show(msg);
                }

            }
            if(dictionaryPathName!="")
                SelectDictionary(dictionaryPathName);
        }

        #region 【内部方法】
        /// <summary>
        /// 【内部方法】主题初始化
        /// </summary>
        /// <param name="_themes"></param>
        private void ThemesInitialization(List<ThemeItem> _themes)
        {
            themes.Clear();
            themes = _themes;
        }
        /// <summary>
        /// 【内部方法】选择主题
        /// </summary>
        /// <param name="dictionaryNewPath">本次要调用的主题路径</param>
        /// <param name="defaultThemeName_In_Xaml">默认主题都含有的字符(如果包含该字符，就算是主题文件)</param>
        private void SelectDictionary(string dictionaryNewPath, string defaultThemeName_In_Xaml = "DictionaryColor")
        {

            // 移除已有主题
            var t = Application.Current.Resources.MergedDictionaries;
            for (int i = 0; i < t.Count; i++)
            {
                var resource = t[i];
                if (resource.Source != null && resource.Source.ToString().Contains(defaultThemeName_In_Xaml))
                {
                    //包含主题文件名称的都将被移除。
                    t.Remove(resource);
                }
            }


            // 加载新主题
            ResourceDictionary themeDictionary = new ResourceDictionary();
            Uri resourceUri = new Uri(dictionaryNewPath, UriKind.Relative);
            themeDictionary.Source = resourceUri;
            t.Add(themeDictionary);

        }
        #endregion



    }
    
    public class ThemeItem
    {
        public ThemeItem() { }
        public string Key { get; set; }
        public string Path { get; set; }
    }

}
